package net.ojbk.study.curriculumDesign.beans.cilentservicer;

import com.alibaba.druid.sql.visitor.functions.Char;
import net.ojbk.study.curriculumDesign.beans.cilentservicer.Chat;
import net.ojbk.study.curriculumDesign.beans.usermanage.User;
import net.ojbk.study.curriculumDesign.dao.cilentservicer.ServicerDao;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: twobox
 * Date: 2019/1/7
 * Time: 15:54
 * Description: No Description
 */
public class ChatSystem {
    private List<Chat> chatBeginList;
    private List<Chat> chatingList;
    private List<Servicer> servicerList;

    private ServicerDao servicerDao;

    /* 构造函数 */
    public ChatSystem(ServicerDao servicerDao) {
        this.servicerDao = servicerDao;
        this.chatBeginList = new ArrayList<Chat>();
        this.chatingList = new ArrayList<Chat>();
        this.servicerList = servicerDao.selectAll();
    }

    /* 以下为功能区 */

    public List<Chat> getAllBeginChat() {
//        System.out.println(this);
//        System.out.println("等待的会话长度:" + this.chatBeginList.size());
//        System.out.println("客服列表" + servicerList);
        return this.chatBeginList;
    }

    public Chat getBeginChatByUser(User user) throws Exception {
        if (user == null || user.getUid() == null)
            throw new Exception("你有问题。User.uid不存在。");
//        System.out.println(this);
//        System.out.println("等待的会话的长度:" + this.chatBeginList.size());
//        System.out.println("地址：");
//        System.out.println("客服列表" + servicerList);
        for (Chat chat : this.chatBeginList) {
            if (chat.getCuser().getUid().equals(user.getUid()))
                return chat;
        }
        throw new Exception("此用户没有等待连接的会话。");
    }

    public Chat getChatingByUser(User user) throws Exception {
        if (user == null || user.getUid() == null)
            throw new Exception("你有问题。User.uid不存在。");
        for (Chat chat : this.chatingList) {
            if (chat.getCuser().getUid().equals(user.getUid()))
                return chat;
        }
        throw new Exception("此用户没有正在通讯的会话。");
    }

    public Servicer findMe(User user) throws Exception {
        if (user == null)
            throw new Exception("你都不是我们的人，怎么可能是客服");
        if (this.servicerList.isEmpty()) {
            this.updateServicerList();
            System.out.println(servicerList);
        }
        for (Servicer servicer : servicerList) {
            if (servicer.getUid().equals(user.getUid()))
                return servicer;
        }
        throw new Exception("你是客服？还让我找你？？");
    }

    public void servicerJoinChat(Servicer servicer, Chat chat) {
//        System.out.println("system.servicerJoinChat：system：" + this);
//        System.out.println("chat.joinServicer(servicer);");
        chat.joinServicer(servicer);
//        System.out.println("servicer.joinChat(chat);");
        servicer.joinChat(chat);
//        System.out.println("this.chatBeginList.remove(chat);");
//        if (this.chatBeginList == null) {
//            System.out.println("this.chatBeginList == null");
//        }
//        System.out.println("删除前的chatBeginList：" + this.chatBeginList);
        this.chatBeginList.remove(chat);
//        System.out.println("删除后的chatBeginList：" + this.chatBeginList);
        this.chatingList.add(chat);
//        System.out.println("添加后的chatingList：" + this.chatingList);
//        System.out.println("删除后" + this.getChatBeginList().size());
//        System.out.println("添加后" + this.getChatingList().size());
    }

    public void updateServicerList() {
        servicerList = servicerDao.selectAll();
    }

    public void addNewBeginChat(Chat chat) {
        this.chatBeginList.add(chat);
    }

    public void catChat(Chat chat) throws Exception{
         // 设置chat状态
        chat.catChat();
        // 移除活动队列
        this.chatingList.remove(chat);
        // 客服对象的sChatList 队列移除该会话
        Servicer theServicer = chat.getServicer();
        theServicer.removeChat(chat);
    }

    public List<Servicer> getServicerList() {
        if (this.servicerList == null || this.servicerList.isEmpty())
            this.servicerList = this.servicerDao.selectAll();
        return servicerList;
    }

    public void setServicerList(List<Servicer> servicerList) {
        this.servicerList = servicerList;
    }

    public List<Chat> getChatBeginList() {
        return this.chatBeginList;
    }

    public void setChatBeginList(List<Chat> chatBeginList) {
        this.chatBeginList = chatBeginList;
    }

    public List<Chat> getChatingList() {
        return chatingList;
    }

    public void setChatingList(List<Chat> chatingList) {
        this.chatingList = chatingList;
    }

}
